package object.parts {
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.library.assets.AssetType;
	import away3d.materials.ColorMaterial;
	import away3d.materials.TextureMaterial;
	import away3d.textures.BitmapTexture;
	import flash.events.Event;
	import loader.LoaderManager;
	
	/**
	 * 轮胎
	 * @author yungzhu@gmail.com
	 * Morn UI：flash轻量级，高性能，可视化UI解决方案 https://code.google.com/p/morn/
	 */
	public class Wheel extends ObjectContainer3D {
		private var _side:String;
		private var _wheel:Mesh;
		private var _rim:Mesh;
		private var _wheelMater:TextureMaterial;
		private var _rimMater:ColorMaterial;
		
		public function Wheel(color:ColorMaterial, side:String = "right") {
			_wheelMater = new TextureMaterial();
			_rimMater = color;
			_side = side;
			LoaderManager.instance.load("models/wheel.png", AssetType.TEXTURE, setTexture, [_wheelMater]);
			LoaderManager.instance.load("models/Wheel.3DS", AssetType.MESH, setMesh, ["_wheel"]);
			LoaderManager.instance.load("models/Rim.3DS", AssetType.MESH, setMesh, ["_rim"]);
			LoaderManager.instance.addEventListener(Event.COMPLETE, onAllLoadComplete);
		}
		
		private function onAllLoadComplete(e:Event):void {
			LoaderManager.instance.removeEventListener(Event.COMPLETE, onAllLoadComplete);
			initParts();
		}
		
		private function initParts():void {
			//轮胎
			_wheelMater.alpha = 1;
			_wheelMater.specular = 0.5;
			_wheel.material = _wheelMater;
			_wheel.rotationY = _side == "left" ? 180 : 0;
			addChild(_wheel);
			//轮壳
			_rim.material = _rimMater;
			_rim.rotationY = _side == "left" ? 180 : 0;
			addChild(_rim);
		}
		
		private function setTexture(mater:TextureMaterial, asset:*):void {
			mater.texture = new BitmapTexture(asset.bitmapData);
		}
		
		private function setMesh(mesh:String, asset:*):void {
			this[mesh] = asset as Mesh;
		}
	}
}